package com.education.admin.modules.dashboard.controller;

import com.education.admin.common.Result;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 仪表盘控制器
 */
@RestController
@RequestMapping("/api/dashboard")
public class DashboardController {
    
    /**
     * 获取仪表盘统计数据
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics() {
        Map<String, Object> data = new HashMap<>();
        
        // 模拟统计数据
        data.put("totalUsers", 1250);
        data.put("totalTeachers", 89);
        data.put("totalStudents", 1161);
        data.put("totalCourses", 234);
        data.put("totalOrders", 567);
        data.put("totalRevenue", 123456.78);
        data.put("todayActiveUsers", 342);
        data.put("monthlyGrowth", 12.5);
        
        return Result.success(data);
    }
    
    /**
     * 获取实时数据看板
     */
    @GetMapping("/realtime")
    public Result<Map<String, Object>> getRealtimeData() {
        Map<String, Object> data = new HashMap<>();
        
        data.put("onlineUsers", 156);
        data.put("todayRegistrations", 23);
        data.put("todayOrders", 45);
        data.put("todayRevenue", 8965.32);
        data.put("serverLoad", 0.65);
        data.put("timestamp", LocalDateTime.now());
        
        return Result.success(data);
    }
    
    /**
     * 获取用户增长趋势
     */
    @GetMapping("/trend/user-growth")
    public Result<Map<String, Object>> getUserGrowthTrend(
            @RequestParam String startDate,
            @RequestParam String endDate) {
        
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> trendData = new ArrayList<>();
        
        // 模拟30天的增长数据
        for (int i = 0; i < 30; i++) {
            Map<String, Object> dayData = new HashMap<>();
            dayData.put("date", "2025-01-" + String.format("%02d", i + 1));
            dayData.put("newUsers", (int)(Math.random() * 50) + 10);
            dayData.put("totalUsers", 1000 + i * 8);
            trendData.add(dayData);
        }
        
        data.put("trend", trendData);
        data.put("summary", Map.of(
            "totalGrowth", 240,
            "averageDaily", 8,
            "growthRate", 12.5
        ));
        
        return Result.success(data);
    }
    
    /**
     * 获取收入趋势
     */
    @GetMapping("/trend/revenue")
    public Result<Map<String, Object>> getRevenueTrend(
            @RequestParam String startDate,
            @RequestParam String endDate) {
        
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> trendData = new ArrayList<>();
        
        // 模拟30天的收入数据
        for (int i = 0; i < 30; i++) {
            Map<String, Object> dayData = new HashMap<>();
            dayData.put("date", "2025-01-" + String.format("%02d", i + 1));
            dayData.put("revenue", (Math.random() * 5000) + 1000);
            dayData.put("orders", (int)(Math.random() * 20) + 5);
            trendData.add(dayData);
        }
        
        data.put("trend", trendData);
        data.put("summary", Map.of(
            "totalRevenue", 95420.50,
            "averageDaily", 3180.68,
            "growthRate", 8.7
        ));
        
        return Result.success(data);
    }
    
    /**
     * 获取地区分布数据
     */
    @GetMapping("/distribution/region")
    public Result<List<Map<String, Object>>> getRegionDistribution() {
        List<Map<String, Object>> data = new ArrayList<>();
        
        String[] provinces = {"北京", "上海", "广东", "江苏", "浙江", "山东", "河南", "四川", "湖北", "福建"};
        for (String province : provinces) {
            Map<String, Object> regionData = new HashMap<>();
            regionData.put("name", province);
            regionData.put("value", (int)(Math.random() * 200) + 50);
            regionData.put("students", (int)(Math.random() * 150) + 30);
            regionData.put("teachers", (int)(Math.random() * 20) + 5);
            data.add(regionData);
        }
        
        return Result.success(data);
    }
    
    /**
     * 获取学生等级分布
     */
    @GetMapping("/distribution/student-level")
    public Result<List<Map<String, Object>>> getStudentLevelDistribution() {
        List<Map<String, Object>> data = new ArrayList<>();
        
        String[] levels = {"初级", "中级", "高级", "专业级"};
        int[] values = {450, 320, 180, 50};
        
        for (int i = 0; i < levels.length; i++) {
            Map<String, Object> levelData = new HashMap<>();
            levelData.put("level", levels[i]);
            levelData.put("count", values[i]);
            levelData.put("percentage", Math.round(values[i] * 100.0 / 1000 * 100.0) / 100.0);
            data.add(levelData);
        }
        
        return Result.success(data);
    }
    
    /**
     * 获取城市渗透率分析
     */
    @GetMapping("/analysis/city-penetration")
    public Result<Map<String, Object>> getCityPenetrationAnalysis() {
        Map<String, Object> data = new HashMap<>();
        
        List<Map<String, Object>> cityData = new ArrayList<>();
        String[] cities = {"北京", "上海", "深圳", "杭州", "成都", "武汉", "西安", "南京"};
        
        for (String city : cities) {
            Map<String, Object> city_data = new HashMap<>();
            city_data.put("city", city);
            city_data.put("penetration", Math.round(Math.random() * 30 + 10));
            city_data.put("userCount", (int)(Math.random() * 5000) + 1000);
            city_data.put("potential", (int)(Math.random() * 10000) + 5000);
            cityData.add(city_data);
        }
        
        data.put("cities", cityData);
        data.put("summary", Map.of(
            "averagePenetration", 18.5,
            "topCity", "上海",
            "growthPotential", "高"
        ));
        
        return Result.success(data);
    }
    
    /**
     * 获取教师审核面板数据
     */
    @GetMapping("/panel/teacher-audit")
    public Result<Map<String, Object>> getTeacherAuditPanelData() {
        Map<String, Object> data = new HashMap<>();
        
        data.put("pendingAudit", 12);
        data.put("auditedToday", 8);
        data.put("rejectedToday", 3);
        data.put("averageAuditTime", "2.5小时");
        
        return Result.success(data);
    }
    
    /**
     * 获取投诉面板数据
     */
    @GetMapping("/panel/complaint")
    public Result<Map<String, Object>> getComplaintPanelData() {
        Map<String, Object> data = new HashMap<>();
        
        data.put("newComplaints", 5);
        data.put("processingComplaints", 12);
        data.put("resolvedToday", 8);
        data.put("satisfactionRate", 92.5);
        
        return Result.success(data);
    }
    
    /**
     * 获取课程排行榜
     */
    @GetMapping("/ranking/course")
    public Result<List<Map<String, Object>>> getCourseRanking(@RequestParam(defaultValue = "10") int limit) {
        List<Map<String, Object>> data = new ArrayList<>();
        
        String[] courses = {"数学基础", "高等数学", "线性代数", "概率统计", "数学分析", "微积分", "代数几何", "数论基础"};
        
        for (int i = 0; i < Math.min(courses.length, limit); i++) {
            Map<String, Object> courseData = new HashMap<>();
            courseData.put("rank", i + 1);
            courseData.put("courseName", courses[i]);
            courseData.put("enrollments", (int)(Math.random() * 500) + 100);
            courseData.put("rating", Math.round((Math.random() * 1 + 4) * 10.0) / 10.0);
            courseData.put("revenue", (Math.random() * 10000) + 5000);
            data.add(courseData);
        }
        
        return Result.success(data);
    }
    
    /**
     * 获取教师排行榜
     */
    @GetMapping("/ranking/teacher")
    public Result<List<Map<String, Object>>> getTeacherRanking(@RequestParam(defaultValue = "10") int limit) {
        List<Map<String, Object>> data = new ArrayList<>();
        
        String[] teachers = {"张老师", "李老师", "王老师", "赵老师", "陈老师", "刘老师", "杨老师", "黄老师"};
        
        for (int i = 0; i < Math.min(teachers.length, limit); i++) {
            Map<String, Object> teacherData = new HashMap<>();
            teacherData.put("rank", i + 1);
            teacherData.put("teacherName", teachers[i]);
            teacherData.put("students", (int)(Math.random() * 100) + 20);
            teacherData.put("rating", Math.round((Math.random() * 1 + 4) * 10.0) / 10.0);
            teacherData.put("income", (Math.random() * 50000) + 10000);
            data.add(teacherData);
        }
        
        return Result.success(data);
    }
}